package com.netistate.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.netistate.base.BaseDao;
import com.netistate.pojo.Doc;

@Repository
public class DocDao extends BaseDao<Doc, Integer>{

	/**
	 * 根据条件去查询文档
	 * @param projectName
	 * @param begintime
	 * @param endtime
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Doc> findDocByCondition(String projectName, String begintime,
			String endtime) {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		begintime = begintime +" 24:00:00";
		
		if("现在".equals(endtime)){
	    	  Date date = new Date();
	    	  endtime = df.format(date);
	    }
		endtime = endtime + " 24:00:00";
		
		String hql = "from Doc where createtime >= '" + begintime + "' and createtime <= '" + endtime +"'";
		if("所有".equals(projectName)){
			hql = hql;
		} else {
			int pid = Integer.parseInt(projectName);
			hql = hql + "and project = '" + pid + "'";
		}
		
		Query query = getSession().createQuery(hql);
		return query.list();
	}

	/**
	 * 切换项目下的文档
	 * @param pid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Doc> findAllDocByPid(int pid) {
		
		String hql = "from Doc where projectid = ?";
		Query query = getSession().createQuery(hql);
		query.setInteger(0, pid);
		
		return query.list();
	}

	/**
	 * 根据psid去删除文档里的记录
	 * @param pssid
	 */
	public void delDocByPSId(int pssid) {
		String hql = "delete Doc where projectServicesid = ?";
		
		Query query = getSession().createQuery(hql);
		query.setParameter(0, pssid);
		
		query.executeUpdate();
		
	}

}
